﻿Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.Utils.Win

Public Class TiepNhanBenhNhan
    Private State As Integer = 0 'trạng thái form 
    '0 mặc định
    '1 tạo mới
    '2 sửa
    '3 xóa
    Private Sub setReadonlyControl(ByVal b As Boolean) 'set readonly để không cho thao tác khi ở trạng thái mặc định - cho thao tác khi ở tạo mới - sửa
        setReadonly(Me.LayoutControl1, b)
        If b Then
            Me.IGridView1.OptionsBehavior.Editable = False
            Me.IGridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        Else
            Me.IGridView1.OptionsBehavior.Editable = False
            Me.IGridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
        End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    'nút thoát
    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'chạy hàm này để giải phóng ram
        Me.Close() 'đóng form
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
    End Sub
    'nút tạo mới
    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        Try
            Me.setReadonlyControl(False)
            Dim row As DataRowView = CType(Me.TiepNhanBenhNhanBindingSource.AddNew, DataRowView)
            row("ma") = "TNBN" & System.Guid.NewGuid().ToString
            Me.LoaibenhnhanISearchLookupEdit.Focus() 'bắt con trỏ ở mã khi tạo mới
            Me.LoaibenhnhanISearchLookupEdit.ShowPopup()
            Me.NgaytiepnhanIDateEdit.EditValue = Today
            Me.GioitinhIComboBoxEdit.EditValue = "Nam"
            Me.QuocgiaISearchLookupEdit.EditValue = "VN"
            Me.DantocISearchLookupEdit.EditValue = "Kinh"
            Me.State = 1 'trạng thái tạo mới
        Catch ex As Exception
            'khi có lỗi - ngoại lệ xảy ra
        End Try

    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DMKhamBenhTableAdapter.Fill(Me.PCV_CLINICDataSet.DMKhamBenh)
        Me.DMPhuongXaTableAdapter.Fill(Me.PCV_CLINICDataSet.DMPhuongXa)
        Me.DMQuanHuyenTableAdapter.Fill(Me.PCV_CLINICDataSet.DMQuanHuyen)
        Me.DMTinhThanhTableAdapter.Fill(Me.PCV_CLINICDataSet.DMTinhThanh)
        Me.DMQuocGiaTableAdapter.Fill(Me.PCV_CLINICDataSet.DMQuocGia)
        Me.DMNganhNgheTableAdapter.Fill(Me.PCV_CLINICDataSet.DMNganhNghe)
        Me.TiepNhanBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet.TiepNhanBenhNhan)
        Me.DMDanTocTableAdapter.Fill(Me.PCV_CLINICDataSet.DMDanToc)
        Me.GetLoaiBenhNHan_TiepNhanBenhNhanTableAdapter.Fill(Me.PCV_CLINICDataSet.getLoaiBenhNHan_TiepNhanBenhNhan)
        setReadonlyControl(True)
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub DM_TinhTrang_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadDuLieu()
        Me.IGridView1.setBatLoi("ma", "not null")
        'Me.IGridView1.setBatLoi("ten", "not null")
        Me.TiepNhanBenhNhanIGridControl.ForceInitialize()
        Me.IGridView1.MoveLast()
        Me.State = 0
    End Sub
    'nút Lưu
    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridView1.PostEditor()
            Me.IGridView1.UpdateCurrentRow()
            Me.IGridView1.CloseEditor()
            Me.TiepNhanBenhNhanBindingSource.EndEdit() 'end edit trước khi lưu - ghi những thay đổi 
            If Me.PCV_CLINICDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TiepNhanBenhNhanTableAdapter.Update(Me.PCV_CLINICDataSet.TiepNhanBenhNhan)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonlyControl(True) 'set lại readonly
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.State = 0
                Me.setReadonlyControl(True) 'set lại readonly
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    'Private Function getKiemTraMaTonTai(ByVal sMa As String) As Boolean 'kiểm tra sự tồn tại của mã - dùng kiểm tra chung với igridview
    '    If Me.TiepNhanBenhNhanBindingSource.Find("ma", sMa) = -1 Then 'nếu -1 nghĩa là ko tìm đc vị trí trong bindingsource
    '        Return False
    '    Else 'còn lại là tìm đc vị trí --> đã tồn tại
    '        Return True
    '    End If
    'End Function
    ''bắt lỗi mã
    'Private Sub MaTextEdit_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SotiepnhanITextEdit.Validating
    '    If State = 1 And Not Me.BarCheckItemNhapNhanh.Checked Then 'xác định chỉ kiểm tra trong trường hợp tạo mới
    '        Try
    '            If Me.getKiemTraMaTonTai(Me.SotiepnhanITextEdit.EditValue) Then 'kiểm tra mã tồn tại hay chưa ?
    '                Me.SotiepnhanITextEdit.ErrorText = "Mã này đã tồn tại"
    '                e.Cancel = True
    '            End If
    '        Catch ex As Exception

    '        End Try
    '    End If
    'End Sub

    'nút sửa
    Private Sub BarButtonItemSua_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonlyControl(False)
        Me.State = 2
        If Not IsDBNull(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
            If Me.LoaibenhnhanISearchLookupEdit.EditValue = "BHYT" Then
                Me.LayoutControlGroup7.Enabled = True
                Me.SobhytITextEdit.Properties.ReadOnly = False
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = False
                Me.NgayhethanIDateEdit.Properties.ReadOnly = False
                Me.TraituyenICheckEdit.Properties.ReadOnly = False
            Else
                Me.LayoutControlGroup7.Enabled = False
                Me.SobhytITextEdit.Properties.ReadOnly = True
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = True
                Me.NgayhethanIDateEdit.Properties.ReadOnly = True
                Me.TraituyenICheckEdit.Properties.ReadOnly = True
            End If
        End If
    End Sub
    'nút first - vị trí đầu tiên
    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.TiepNhanBenhNhanBindingSource.MoveFirst()
    End Sub
    'nút previous - vị trí trước 
    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonPrevious.ItemClick
        Me.TiepNhanBenhNhanBindingSource.MovePrevious()
    End Sub
    'nút next - vị trí tiếp theo
    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNext.ItemClick
        Me.TiepNhanBenhNhanBindingSource.MoveNext()
    End Sub
    'nút last - vị trí cuối cùng
    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonLast.ItemClick
        Me.TiepNhanBenhNhanBindingSource.MoveLast()
    End Sub
    'nút xóa 
    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.SotiepnhanITextEdit.EditValue & "' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Try
                Me.TiepNhanBenhNhanBindingSource.RemoveCurrent()
                Me.TiepNhanBenhNhanTableAdapter.Update(Me.PCV_CLINICDataSet.TiepNhanBenhNhan)
                Me.State = 0
                Me.setReadonlyControl(True)
            Catch ex As Exception

            End Try

        End If
    End Sub
    'hủy thay đổi 
    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.TiepNhanBenhNhanBindingSource.EndEdit()
        If Me.PCV_CLINICDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_CLINICDataSet.RejectChanges()
                Me.State = 0
                Me.setReadonlyControl(True)
                'Me.setButtonTrangThaiSua(False)
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.State = 0
            Me.setReadonlyControl(True)

            'Me.setButtonTrangThaiSua(False)
        End If

    End Sub
    'nút in 
    Private Sub BarButtonItemIn_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemIn.ItemClick
        Dim report As rp_TiepNhanBenhNhan = New rp_TiepNhanBenhNhan
        report.TiepNhanBenhNhanTableAdapter1.FillMa_TiepNhanBenhNhan(report.PcV_CLINICDataSet1.TiepNhanBenhNhan, Me.IGridView1.GetFocusedRowCellValue("ma"))
        report.Ngay.Value = Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue).Date, "dd")
        report.Thang.Value = Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue).Month, "00")
        report.Nam.Value = Format(CDate(Me.NgaytiepnhanIDateEdit.EditValue).Year, "0000")
        If IsDBNull(Me.NoigioithieuITextEdit.EditValue) OrElse Me.NoigioithieuITextEdit.EditValue.ToString.Trim = "" Then
            report.Tuden.Value = "Tự đến"
        Else
            report.Tuden.Value = "Chuyển viện"
        End If
        report.ShowRibbonPreview() 'hiển thị report theo kiểu ribbon
    End Sub

    ''kiểm tra giá trị cột đang thao tác 
    'Private Sub IGridView1_ValidatingEditor(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.BaseContainerValidateEditorEventArgs) Handles IGridView1.ValidatingEditor
    '    If Me.IGridView1.FocusedColumn.FieldName = "ma" And IGridView1.FocusedRowHandle <> -999997 Then
    '        If Me.getKiemTraMaTonTai(e.Value) And State = 1 Then
    '            e.ErrorText = "Mã '" & e.Value & "'đã tồn tại !"
    '            e.Valid = False
    '        End If
    '    End If
    'End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Dim position As Integer = Me.TiepNhanBenhNhanBindingSource.Position
        Me.State = 0
        loadDuLieu()
        Me.TiepNhanBenhNhanBindingSource.Position = position

    End Sub

    'Private Sub QuocgiaISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles QuocgiaISearchLookupEdit.EditValueChanging
    '    Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
    '    Dim rowhandle As Integer = Me.QuocgiaISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
    '    Me.DMTinhThanhBindingSource.Filter = "maquocgia='" & editor.Properties.View.GetRowCellValue(rowhandle, "ma") & "'"
    'End Sub

    'Private Sub TinhthanhISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles TinhthanhISearchLookupEdit.EditValueChanging
    '    Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
    '    Dim rowhandle As Integer = Me.TinhthanhISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
    '    Me.DMQuanHuyenBindingSource.Filter = "matinhthanh='" & editor.Properties.View.GetRowCellValue(rowhandle, "ma") & "'"
    'End Sub

    'Private Sub QuanhuyenISearchLookupEdit_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles QuanhuyenISearchLookupEdit.EditValueChanging
    '    Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
    '    Dim rowhandle As Integer = Me.QuanhuyenISearchLookupEdit.Properties.GetIndexByKeyValue(e.NewValue)
    '    Me.DMPhuongXaBindingSource.Filter = "maquanhuyen='" & editor.Properties.View.GetRowCellValue(rowhandle, "ma") & "'"
    'End Sub
    Private Sub TinhthanhISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles TinhthanhISearchLookupEdit.Enter
        Me.DMTinhThanhBindingSource.Filter = "maquocgia='" & Me.QuocgiaISearchLookupEdit.EditValue.ToString & "'"
    End Sub

    Private Sub QuanhuyenISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles QuanhuyenISearchLookupEdit.Enter
        Me.DMQuanHuyenBindingSource.Filter = "matinhthanh='" & Me.TinhthanhISearchLookupEdit.EditValue.ToString & "'"
    End Sub

    Private Sub PhuongxaISearchLookupEdit_Enter(sender As Object, e As System.EventArgs) Handles PhuongxaISearchLookupEdit.Enter
        Me.DMPhuongXaBindingSource.Filter = "maquanhuyen='" & Me.QuanhuyenISearchLookupEdit.EditValue.ToString & "'"
    End Sub

    Private Sub LoaibenhnhanISearchLookupEdit_EditValueChanged(sender As Object, e As System.EventArgs) Handles LoaibenhnhanISearchLookupEdit.EditValueChanged
        If State <> 0 And Not IsDBNull(Me.LoaibenhnhanISearchLookupEdit.EditValue) Then
            If Me.LoaibenhnhanISearchLookupEdit.EditValue = "BHYT" Then
                Me.LayoutControlGroup7.Enabled = True
                Me.SobhytITextEdit.Properties.ReadOnly = False
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = False
                Me.NgayhethanIDateEdit.Properties.ReadOnly = False
                Me.TraituyenICheckEdit.Properties.ReadOnly = False
            Else
                Me.LayoutControlGroup7.Enabled = False
                Me.SobhytITextEdit.Properties.ReadOnly = True
                Me.NgaybatdauIDateEdit.Properties.ReadOnly = True
                Me.NgayhethanIDateEdit.Properties.ReadOnly = True
                Me.TraituyenICheckEdit.Properties.ReadOnly = True
            End If
        End If
    End Sub

    Private Sub DiachiITextEdit_GotFocus(sender As Object, e As System.EventArgs) Handles DiachiITextEdit.GotFocus
        If State <> 0 And Not (IsDBNull(Me.PhuongxaISearchLookupEdit.EditValue) AndAlso IsDBNull(Me.QuanhuyenISearchLookupEdit.EditValue) AndAlso IsDBNull(Me.TinhthanhISearchLookupEdit.EditValue) AndAlso IsDBNull(Me.ThonphoduongITextEdit.EditValue) AndAlso IsDBNull(Me.SonhaITextEdit.EditValue)) Then
            Me.DiachiITextEdit.EditValue = " " & Me.SonhaITextEdit.EditValue & ", " & Me.ThonphoduongITextEdit.EditValue & ", " & Trim(Me.PhuongxaISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.PhuongxaISearchLookupEdit.EditValue)) & ", " & Trim(Me.QuanhuyenISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.QuanhuyenISearchLookupEdit.EditValue)) & ", " & Trim(Me.TinhthanhISearchLookupEdit.Properties.GetDisplayValueByKeyValue(Me.TinhthanhISearchLookupEdit.EditValue))
            Me.DiachiITextEdit.EditValue = LTrim(Me.DiachiITextEdit.EditValue.ToString.Replace(" ,", ""))
        End If
    End Sub

    Private Sub GioitinhIComboBoxEdit_GotFocus(sender As Object, e As System.EventArgs) Handles GioitinhIComboBoxEdit.GotFocus
        If State = 1 Then
            Me.GioitinhIComboBoxEdit.ShowPopup()
        End If
    End Sub

End Class